﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccompanyCabinet.CabinetControl
{
    public partial class KeyBoard : Form
    {
        private static KeyBoard kbf = null;
        public KeyBoard()
        {
            InitializeComponent();
        }
        public static KeyBoard CreateInstrance()
        {
            if (kbf == null || kbf.IsDisposed)
            {
                kbf = new KeyBoard();
            }
            return kbf;
        }
        private const int WS_EX_NOACTIVATE = 0x08000000;
        private const int WM_MOUSEACTIVATE = 0x0021;
        private const int MA_NOACTIVATE = 0x0003;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_NOACTIVATE;
                return cp;
            }

        }
        protected override void WndProc(ref Message m)
        {
            //被激活
            if (m.Msg == WM_MOUSEACTIVATE)
            {
                //拒绝激活
                m.Result = (IntPtr)MA_NOACTIVATE;
            }
            else
            {
                base.WndProc(ref m);
            }
        }
        private void KeyBoard_Load(object sender, EventArgs e)
        {
            pb_0.Click += new EventHandler(Pb_Click);
            pb_1.Click += new EventHandler(Pb_Click);
            pb_2.Click += new EventHandler(Pb_Click);
            pb_3.Click += new EventHandler(Pb_Click);
            pb_4.Click += new EventHandler(Pb_Click);
            pb_5.Click += new EventHandler(Pb_Click);
            pb_6.Click += new EventHandler(Pb_Click);
            pb_7.Click += new EventHandler(Pb_Click);
            pb_8.Click += new EventHandler(Pb_Click);
            pb_9.Click += new EventHandler(Pb_Click);
            pb_Dot.Click += new EventHandler(Pb_Click);
            pb_BACKSPACE.Click += new EventHandler(Pb_Click);
        }
        void Pb_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            string strKey = pb.Name.Substring(3);
            if (strKey.Equals("Dot"))
            {
                SendKeys.Send("{.}");
            }
            else
            {
                SendKeys.Send("{" + strKey + "}");
            }
        }
        /// <summary>
        /// 关闭键盘
        /// </summary>
        public static void HideKbf()
        {
            kbf.Close();
        }
    }
}
